﻿#ifndef CHATCONTENT_H
#define CHATCONTENT_H

#include <QPixmap>
#include <QTextDocument>
#include <QVariant>

class QPainter;

class ChatContent
{
public:
    ChatContent() {}
    virtual ~ChatContent(){}
    virtual QVariant data(int role = Qt::DisplayRole) const = 0;
};

class ChatPlainText : public ChatContent
{
public:
    explicit ChatPlainText(const QString &text);
    QVariant data(int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
private:
    QString text;
};

class ChatDirection : public ChatContent
{
public:
    ChatDirection(const bool from, const QPixmap &icon, const QVariant &display = QVariant(), const QString &extra = QString());
    QVariant data(int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    QByteArray mid;
    const bool from;    //true:别人发的;false:我发的
protected:
    const QPixmap icon;
    QVariant display;
public:
    QString extraText;  //自己:状态，别人:名字
};

class ChatImage : public ChatDirection
{
public:
    ChatImage(const bool from, const QPixmap &icon, const QUrl &url, bool &requestDownload, const QString &extra = QString());
    ChatImage(const QPixmap &icon, const QPixmap &pix);
    QVariant data(int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    QString getUrl() const;
    void downloadFinished(const QPixmap &pixmap, const QByteArray &md5);
    void downloadError();
    void downloadProgress(int progress);
private:
    static const QString brokenImage;
    static const QString loadingImageFormat;
    void handleOriginal(bool createMd5);
    void createMd5();
    void createShow();
    QPixmap original;
    QByteArray md5;
    QString url;
    bool widthSet;
};

#endif // CHATCONTENT_H
